package com.jinzheng.qhnetty.commont.waitnetty;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@ChannelHandler.Sharable
@Slf4j
public class ServerHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String data) throws Exception {
        //TODO自己的业务逻辑
        System.out.println(data);
        ctx.channel().writeAndFlush("from server: " + UUID.randomUUID());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        updateState(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        updateState(ctx);
    }

    /**
     * @param ctx
     */
    private void updateState(ChannelHandlerContext ctx) {
        //TODO业务逻辑
    }

}
